package com.ruoyi.record.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.basic.domain.BasicUser;
import com.ruoyi.basic.service.IBasicDutyService;
import com.ruoyi.basic.service.IBasicUserService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.record.domain.RecordSalaryContent;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.record.domain.RecordLeave;
import com.ruoyi.record.service.IRecordLeaveService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 请假记录Controller
 * 
 * @author ruoyi
 * @date 2022-11-10
 */
@Controller
@RequestMapping("/record/leave")
public class RecordLeaveController extends BaseController
{
    private String prefix = "record/leave";

    @Autowired
    private IRecordLeaveService recordLeaveService;

    @Autowired
    private IBasicUserService basicUserService;

    @RequiresPermissions("record:leave:view")
    @GetMapping()
    public String leave(ModelMap mmap)
    {
        if(ShiroUtils.getSysUser().isAdmin() || ShiroUtils.getSubject().hasRole("administrator")) {
            mmap.put("isAdmin", true);
        }
        return prefix + "/leave";
    }

    /**
     * 查询请假记录列表
     */
    @RequiresPermissions("record:leave:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordLeave recordLeave)
    {
        startPage();
        if(!ShiroUtils.getSubject().hasRole("administrator")) {
            if (ShiroUtils.getSubject().hasRole("timekeeper")) {
                recordLeave.setDeptId(ShiroUtils.getDeptId());
            } else if (ShiroUtils.getSubject().hasRole("common")) {
                recordLeave.setCreateBy(ShiroUtils.getLoginName());
            }
        }
        List<RecordLeave> list = recordLeaveService.selectRecordLeaveList(recordLeave);
        return getDataTable(list);
    }

    /**
     * 导出请假记录列表
     */
    @Log(title = "请假记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecordLeave recordLeave)
    {
        if(!ShiroUtils.getSubject().hasRole("administrator")) {
            if (ShiroUtils.getSubject().hasRole("timekeeper")) {
                recordLeave.setDeptId(ShiroUtils.getDeptId());
            } else if (ShiroUtils.getSubject().hasRole("common")) {
                recordLeave.setCreateBy(ShiroUtils.getLoginName());
            }
        }
        List<RecordLeave> list = recordLeaveService.selectRecordLeaveList(recordLeave);
        ExcelUtil<RecordLeave> util = new ExcelUtil<RecordLeave>(RecordLeave.class);
        return util.exportExcel(list, "请假记录");
    }

    /**
     * 新增请假记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存请假记录
     */
    @RequiresPermissions("record:leave:add")
    @Log(title = "请假记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecordLeave recordLeave)
    {
        recordLeave.setDeptId(ShiroUtils.getDeptId());
        recordLeave.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recordLeaveService.insertRecordLeave(recordLeave));
    }

    /**
     * 修改请假记录
     */
    @GetMapping("/edit/{leaveId}")
    public String edit(@PathVariable("leaveId") Long leaveId, ModelMap mmap)
    {
        RecordLeave recordLeave = recordLeaveService.selectRecordLeaveByLeaveId(leaveId);
        mmap.put("recordLeave", recordLeave);
        return prefix + "/edit";
    }

    /**
     * 修改保存请假记录
     */
    @RequiresPermissions("record:leave:edit")
    @Log(title = "请假记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecordLeave recordLeave)
    {
        recordLeave.setDeptId(ShiroUtils.getDeptId());
        recordLeave.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recordLeaveService.updateRecordLeave(recordLeave));
    }

    /**
     * 删除请假记录
     */
    @RequiresPermissions("record:leave:remove")
    @Log(title = "请假记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recordLeaveService.deleteRecordLeaveByLeaveIds(ids));
    }

    /**
     * 提交请假审核
     */
    @GetMapping("/submit/{leaveId}")
    public String submit(@PathVariable("leaveId") Long leaveId, ModelMap mmap)
    {
        RecordLeave recordLeave = recordLeaveService.selectRecordLeaveByLeaveId(leaveId);
        mmap.put("recordLeave", recordLeave);
        return prefix + "/submit";
    }

    /**
     * 请假记录提交
     */
    @RequiresPermissions("record:leave:submit")
    @Log(title = "请假记录", businessType = BusinessType.OTHER)
    @PostMapping("/submit")
    @ResponseBody
    public AjaxResult submitSave(RecordLeave recordLeave)
    {
        recordLeave.setVerifyFlag("1");
        return toAjax(recordLeaveService.updateRecordLeave(recordLeave));
    }

    /**
     * 修改请假记录
     */
    @GetMapping("/print/{leaveId}")
    public String print(@PathVariable("leaveId") Long leaveId, ModelMap mmap)
    {
        RecordLeave recordLeave = recordLeaveService.selectRecordLeaveByPrint(leaveId);
        mmap.put("recordLeave", recordLeave);
        if(recordLeave.getUserType().equals("01")) {
            return prefix + "/print";
        }else {
            return prefix + "/print1";
        }
    }

    /**
     * 打印
     */
    @Log(title = "请假记录", businessType = BusinessType.OTHER)
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult update(Long leaveId)
    {
        recordLeaveService.update(null, new LambdaUpdateWrapper<RecordLeave>().set(RecordLeave::getPrintFlag, "1").eq(RecordLeave::getLeaveId, leaveId));
        return success();
    }

    /**
     * 撤销打印
     */
    @RequiresPermissions("record:leave:cancel")
    @Log(title = "请假记录", businessType = BusinessType.OTHER)
    @PostMapping("/cancel")
    @ResponseBody
    public AjaxResult cancel(Long leaveId)
    {
        recordLeaveService.update(null, new LambdaUpdateWrapper<RecordLeave>().set(RecordLeave::getPrintFlag, "0").eq(RecordLeave::getLeaveId, leaveId));
        return success();
    }

    @RequiresPermissions("record:verify:view")
    @GetMapping("/verify/{verifyType}")
    public String verify(@PathVariable("verifyType") String verifyType, ModelMap mmap)
    {
        BasicUser basicUser = basicUserService.selectBasicUserByIdCard(ShiroUtils.getLoginName());
        if(StringUtils.isNotNull(basicUser)) {
            mmap.put(verifyType + "Id", basicUser.getId());
        }
        mmap.put("verifyType", verifyType);
        return prefix + "/verify/leave";
    }

    /**
     * 查询请假记录列表
     */
    @RequiresPermissions("record:verify:list")
    @PostMapping("/verify/list")
    @ResponseBody
    public TableDataInfo verifyList(RecordLeave recordLeave)
    {
        startPage();
        List<RecordLeave> list = recordLeaveService.selectVerifyLeaveList(recordLeave);
        return getDataTable(list);
    }

    /**
     * 提交请假审核
     */
    @GetMapping("/verify/verify/{leaveId}/{verifyType}")
    public String verify(@PathVariable("leaveId") Long leaveId, @PathVariable("verifyType") String verifyType, ModelMap mmap)
    {
        RecordLeave recordLeave = recordLeaveService.selectRecordLeaveByLeaveId(leaveId);
        mmap.put("recordLeave", recordLeave);
        mmap.put("verifyType", verifyType);
        return prefix + "/verify/verify";
    }

    /**
     * 请假记录审批
     */
    @RequiresPermissions("record:verify:save")
    @Log(title = "请假记录", businessType = BusinessType.OTHER)
    @PostMapping("/verify/save")
    @ResponseBody
    public AjaxResult verifySave(RecordLeave recordLeave)
    {
        recordLeave.setVerifyFlag("2");
        if(recordLeave.getVerifyType().equals("chief")) {
            recordLeave.setChiefTime(DateUtils.getNowDate());
        } else if(recordLeave.getVerifyType().equals("manage")) {
            recordLeave.setManageTime(DateUtils.getNowDate());
        } else if(recordLeave.getVerifyType().equals("human")) {
            recordLeave.setHumanTime(DateUtils.getNowDate());
        } else if(recordLeave.getVerifyType().equals("leader")) {
            recordLeave.setLeaderTime(DateUtils.getNowDate());
        } else if(recordLeave.getVerifyType().equals("admin")) {
            recordLeave.setAdminTime(DateUtils.getNowDate());
        } else if(recordLeave.getVerifyType().equals("party")) {
            recordLeave.setPartyTime(DateUtils.getNowDate());
        }
        return toAjax(recordLeaveService.updateRecordLeave(recordLeave));
    }
}
